{extend name="public:head"}
{block name="main"}

<form class="layui-form" action="" style="margin-top: 50px" lay-filter="formAll">

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">IP黑名单</label>
        <div class="layui-input-block">
            <textarea name="desc" placeholder="用%隔开每一个IP" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <!--<a href="/login/Preview/index" class="layui-btn layui-btn-warm" lay-submit lay-filter="yulan">预览</a>-->
        </div>
    </div>

</form>

<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        var token = layui.data('cache');

        //  初始化值
        $.post({
            url:"/login/Identification/Initialization?type=ip",
            type:"get",
            headers:{"token":token.token},
            dataType:"json",
            success:function (res) {
                var ip_str = res.data.ip
                var ip = ip_str.replace(/"/g, '');
                form.val("formAll",{
                    "desc":ip
                })
                if(res.code == 400){
                    layer.msg("初始失败，刷新")
                }
            },
            error:function (error) {
                if(error.code == 500){
                    layer.msg('权限不够');
                }
            }
        })
        //监听提交
        form.on('submit(formDemo)', function(data){
            var textData = JSON.stringify(data.field.desc);
            var ip = textData.replace(/\"/g, '');
            console.log(ip);
            $.post({
                url:"/login/Identification/identificationAndIpApi",
                type:"post",
                headers:{"token":token.token},
                dataType:"json",
                data:{"data":ip,"type":"ip"},
                success:function (res) {
                    if(res.code == 200){
                        layer.msg("更新成功")
                    }
                    if(res.code == 400){
                        layer.msg("更新失败")
                    }
                },
                error:function (error) {
                    if(error.code == 500){
                        layer.msg('权限不够');
                    }
                }
            })
            return false;
        });
    });
</script>

{/block}